#pragma once
#include "stdafx.h"
#include "myRegister.h"

myRegister::myRegister()
{

}

myRegister::~myRegister()
{

}

int myRegister::Query(HKEY hHandle, char *Path, char *Title, char *Buf, int BufSize)
{
	HKEY hKey = NULL;
	int Ret = 0;

	Ret = RegOpenKeyEx(hHandle, Path, 0, KEY_READ, &hKey);
	if (ERROR_SUCCESS != Ret)
	{
		return -1;
	}

	DWORD dwData = 255;
	DWORD dwType = 0;
	char szBuffer[1024] = {0};

	Ret = RegQueryValueEx(hKey, Title, NULL, &dwType, (BYTE*)szBuffer, &dwData);
	if (ERROR_SUCCESS != Ret)
	{
		RegCloseKey(hKey);
		return -1;
	}

	RegCloseKey(hKey);

	int Lenth = strlen(szBuffer);
	if (BufSize < Lenth)
	{
		return -1;
	}

	memcpy(Buf, szBuffer, sizeof(char) * Lenth);
	return 1;
}

int myRegister::Edit(HKEY hHandle, char *Path, char *Title, char *Value)
{
	HKEY hKey = NULL;
	int Ret = 0;

	Ret = RegOpenKeyEx(hHandle, Path, 0, KEY_WRITE, &hKey);
	if (ERROR_SUCCESS != Ret)
	{
		return -1;
	}

	Ret = RegSetValueEx(hKey, Title, NULL, REG_SZ, (BYTE*)Value, strlen(Value));
	if (ERROR_SUCCESS != Ret)
	{
		RegCloseKey(hKey);
		return -1;
	}

	RegCloseKey(hKey);
	return 1;
}